import { request } from '@umijs/max';

export interface HelpDocument {
  id: number;
  title: string;
  content: string;
  url: string;
  category?: string;
  parentId?: number;
  sortOrder: number;
  viewCount: number;
  isPublished: boolean;
  summary?: string;
  createdAt: string;
  updatedAt: string;
}

export interface HelpDocumentResponse {
  success: boolean;
  data: HelpDocument | HelpDocument[];
  message?: string;
}

export interface SearchResponse {
  success: boolean;
  data: HelpDocument[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

// API 基础地址 - 使用代理或直接地址
// 开发环境使用代理 /help-docs-api，生产环境需要配置完整URL
const HELP_DOCS_API_BASE = 
  process.env.REACT_APP_HELP_DOCS_API || 
  (process.env.NODE_ENV === 'development' ? '/help-docs-api' : 'http://localhost:3001/api/help-docs');

/**
 * 获取所有已发布的帮助文档
 */
export async function getAllHelpDocs(options?: { [key: string]: any }) {
  return request<HelpDocumentResponse>(HELP_DOCS_API_BASE, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 根据ID获取帮助文档
 */
export async function getHelpDocById(id: number, options?: { [key: string]: any }) {
  return request<HelpDocumentResponse>(`${HELP_DOCS_API_BASE}/${id}`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 根据URL获取帮助文档
 */
export async function getHelpDocByUrl(url: string, options?: { [key: string]: any }) {
  return request<HelpDocumentResponse>(`${HELP_DOCS_API_BASE}/url/${encodeURIComponent(url)}`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 根据分类获取帮助文档
 */
export async function getHelpDocsByCategory(category: string, options?: { [key: string]: any }) {
  return request<HelpDocumentResponse>(`${HELP_DOCS_API_BASE}/category/${category}`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 获取根文档列表
 */
export async function getRootHelpDocs(options?: { [key: string]: any }) {
  return request<HelpDocumentResponse>(`${HELP_DOCS_API_BASE}/root/list`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 根据父ID获取子文档
 */
export async function getChildHelpDocs(parentId: number, options?: { [key: string]: any }) {
  return request<HelpDocumentResponse>(`${HELP_DOCS_API_BASE}/parent/${parentId}`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 搜索帮助文档
 */
export async function searchHelpDocs(
  keyword: string,
  page: number = 1,
  pageSize: number = 20,
  options?: { [key: string]: any }
) {
  return request<SearchResponse>(
    `${HELP_DOCS_API_BASE}/search/${encodeURIComponent(keyword)}?page=${page}&pageSize=${pageSize}`,
    {
      method: 'GET',
      ...(options || {}),
    }
  );
}

/**
 * 创建帮助文档
 */
export async function createHelpDoc(data: Partial<HelpDocument>, options?: { [key: string]: any }) {
  return request<HelpDocumentResponse>(HELP_DOCS_API_BASE, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data,
    ...(options || {}),
  });
}

/**
 * 更新帮助文档
 */
export async function updateHelpDoc(id: number, data: Partial<HelpDocument>, options?: { [key: string]: any }) {
  return request<HelpDocumentResponse>(`${HELP_DOCS_API_BASE}/${id}`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    data,
    ...(options || {}),
  });
}

/**
 * 删除帮助文档
 */
export async function deleteHelpDoc(id: number, options?: { [key: string]: any }) {
  return request<HelpDocumentResponse>(`${HELP_DOCS_API_BASE}/${id}`, {
    method: 'DELETE',
    ...(options || {}),
  });
}

